import {
  toLine
} from '@/utils'
export default {
  props: {
    /**
     * id的名称
     */
    idName: {
      type: String,
      default: 'id'
    }
  },
  data() {
    return {
      initSearch: true,
      auditStatusName: 'status',
      /**
       * 表格的默认key
       */
      tableKey: 'id',
      /**
       * 列表数据
       */
      list: [],
      /**
       * 选中的列表
       */
      selectionList: [],
      /**
       * 选中的列表的id集合
       */
      selectionIds: [],
      /**
       * 删除api方法名
       */
      deleteName: 'del',
      confirmDeleteInfo: '确定要删除吗？',
      /**
       * 批量删除加载
       */
      batchDeleteLoading: false,
      dialogAuditVisible: false,
      /**
       * 删除api方法名
       */
      exportName: 'exports',
      /**
       * 查询api方法名
       */
      listName: 'list',
      /**
       * 批量删除名
       */
      batchDeleteName: 'batchDelete',
      /**
       * 查询form ref
       */
      searchFormId: 'searchForm',
      /**
       * 对应的api文件对象
       */
      /**
       * 删除加载
       */
      deleteLoading: false,
      /**
       * 总数
       */
      total: 0,
      /**
       * 列表加载
       */
      listLoading: true,
      /**
       * 编辑或查看详情的id值
       */
      editId: '',
      editId1: '',
      auditCompleteArray: ['2', '3'],
      /**
       * 导出参数
       */
      oldParams: {

      },
      /**
       * 查询参数
       */
      listQuery: {
        pageNum: 1,
        pageSize: 30,
        orderBy: 'update_time desc'
      },
      /**
       *  保存编辑框显示隐藏
       */
      dialogFormVisible: false,
      /**
       * 下载加载
       */
      downloadLoading: false
    }
  },
  methods: {
    /**
     * 批量删除
     */
    batchDelete() {
      if (!this.selectionIds || !this.selectionIds.length) {
        this.$message.error('请至少选择一项！')
        return
      }
      const confirmDelete = this.confirmDeleteInfo
      const warning = '提示'
      const confirm = '确定'
      const cancel = '取消'
      if (this.beforeBatchDelete) {
        this.beforeBatchDelete()
      }
      this.$confirm(
        confirmDelete,
        warning, {
          confirmButtonText: confirm,
          cancelButtonText: cancel,
          type: 'warning'
        }
      )
        .then(() => {
          const batchDelete = this.api[this.batchDeleteName]
          this.batchDeleteLoading = true
          batchDelete(this.selectionIds).then(res => {
            this.batchDeleteLoading = false
            this.$nextTick(() => {
              this.handleFilter()
              if (this.batchDelCallback) {
                this.batchDelCallback()
              }
            })
          }).catch(e => {
            this.batchDeleteLoading = false
          })
        })
    },
    /**
     * 重置查询表单
     */
    reset() {
      this.$refs[this.searchFormId].resetFields()
    },
    /**
     * 表格选择改变时触发
     * @param {*} val
     */
    selectionChange(val) {
      this.selectionList = []
      this.selectionIds = []
      if (!val || !val.length) {
        return
      }
      val.forEach(item => {
        if (this.selectionIds[this.idName]) {
          return
        }
        this.selectionIds.push(item[this.idName])
        this.selectionList.push(item)
      })
    },
    /**
     * 查询方法
     */
    getList() {
      const list = this.api[this.listName]
      if (this.beforeQuery) {
        this.beforeQuery()
      }
      this.listLoading = true
      this.oldParams = {
        ...this.listQuery,
        pageNum: null,
        pageSize: null
      }
      list(this.listQuery).then(res => {
        this.listLoading = false
        if (!res) {
          this.list = []
          return
        }
        const {
          list,
          total
        } = res
        if (list) {
          this.list = list
          this.total = total
        } else {
          this.list = res
        }
        if (this.callback) {
          this.callback(list || [])
        }
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    /**
     * 过滤方法
     */
    handleFilter() {
      if (this.listQuery.pageNum) {
        this.listQuery.pageNum = 1
      }
      this.getList()
    },
    /**
     * 编辑或保存关闭时触发
     */
    handlerClose() {
      this.dialogFormVisible = false
      this.editId = ''
      if (this.closeCallback) {
        this.closeCallback()
      }
    },
    handlerClose1() {
      this.dialogFormVisible1 = false
      this.editId1 = ''
      if (this.closeCallback) {
        this.closeCallback()
      }
    },
    /**
     * 列排序规则发生改变触发
     * @param {*} data
     */
    sortChange(data) {
      const {
        prop,
        order
      } = data
      if (order === 'ascending') {
        this.listQuery.orderBy = toLine(prop)
      } else {
        this.listQuery.orderBy = toLine(prop) + ' desc'
      }
      this.handleFilter()
    },
    /**
     * 打开创建弹窗
     */
    handleCreate() {
      this.editId = ''
      this.dialogFormVisible = true
      if (this.handleCreateCallback) {
        this.handleCreateCallback()
      }
    },
    /**
     * 打开编辑弹框
     * @param {*} row
     */
    handleUpdate(row) {
      this.dialogFormVisible = true
      this.editId = row[this.idName]
      if (this.handleUpdateCallback) {
        this.handleUpdateCallback(row)
      }
    },
    handleUpdate1(row) {
      this.dialogFormVisible1 = true
      this.editId1 = row[this.idName]
      if (this.handleUpdateCallback) {
        this.handleUpdateCallback(row)
      }
    },
    /**
     * 单行删除
     * @param {*} row
     */
    handleDelete(row) {
      const del = this.api[this.deleteName]
      const confirmDelete = this.confirmDeleteInfo
      const warning = '提示'
      const confirm = '确定'
      const cancel = '取消'
      if (this.beforeDelete) {
        this.beforeDelete(row)
      }
      this.$confirm(
        confirmDelete,
        warning, {
          confirmButtonText: confirm,
          cancelButtonText: cancel,
          type: 'warning'
        }
      )
        .then(() => {
          this.deleteLoading = true
          del(row[this.idName]).then(msg => {
            this.deleteLoading = false
            this.$nextTick(() => {
              this.handleFilter()
              if (this.delCallback) {
                this.delCallback(row)
              }
            })
          }).catch(e => {
            this.deleteLoading = false
          })
        })
        .catch(() => { })
    },
    /**
     * 导出excel
     */
    handleExport() {
      if (!this.list || !this.list.length) {
        this.$message.error('列表没有数据不能导出！')
        return
      }
      if (this.beforeExport) {
        this.beforeExport()
      }
      const exports = this.api[this.exportName]
      exports(this.oldParams).then(data => {
        const a = document.createElement('a')
        a.href = URL.createObjectURL(data)
        a.download = this.exportFileName
        a.click()
        if (this.exprotCallback) {
          this.exprotCallback()
        }
      })
    }
  },
  computed: {
    tablelHeight() {
      return window.innerHeight > 750
        ? window.innerHeight - 360 : window.innerHeight - 272
    }
  },
  mounted() {
    if (!this.api) {
      return
    }
    if (this.initSearch) {
      this.getList()
    }
  }
}
